package com.ian.departmentmanager.mvc;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.ian.departmentmanager.bo.Member;
import com.ian.departmentmanager.service.MemberService;
import com.ian.departmentmanager.util.JsonUtil;

@Controller
public class AjaxController {

	private MemberService memberSerivce;

	@Autowired
	public void setMemberSerivce(MemberService memberSerivce) {
		this.memberSerivce = memberSerivce;
	}

	@RequestMapping(value = "/ajax/connect.htm", method = RequestMethod.GET)
	protected void sendMail(
			@RequestParam(value = "deptID") String deptID,
			HttpServletRequest request,HttpServletResponse response) throws IOException {

		List<Member> members = memberSerivce.getMembers();
		List<String> emailsList = new ArrayList<String>();
		for (Member member : members) {
			emailsList.add(member.getEmail());
		}
		String emails = JsonUtil.list2json(emailsList);
		response.getWriter().write(emails);
	}

}
